<template>
  <div class="comment-input-area">
    <img v-if="currentUser" v-img-error :src="currentUser.avatar" alt="用户头像" class="user-avatar">
    <img v-else v-img-error src="https://placehold.co/40x40/CCCCCC/FFFFFF?text=游客" alt="游客头像" class="user-avatar">
    <div class="comment-input-wrapper">
      <textarea 
        v-model="commentText"
        :placeholder="placeholder"
        class="comment-input"
        :rows="rows"
        :disabled="isSubmitting"
        @keydown.ctrl.enter="submitComment"
      ></textarea>
      <div class="comment-actions">
        <div class="emoji-actions">
          <button 
            v-for="emoji in quickEmojis" 
            :key="emoji" 
            class="emoji-btn"
            :title="`插入${emoji}`"
            @click="addEmoji(emoji)"
          >
            {{ emoji }}
          </button>
        </div>
        <button 
          class="submit-comment-btn" 
          :disabled="!canSubmit || isSubmitting" 
          @click="submitComment"
        >
          <span v-if="isSubmitting">发送中...</span>
          <span v-else>发送评论</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CommentInput',
  props: {
    currentUser: {
      type: Object,
      default: null,
    },
    placeholder: {
      type: String,
      default: '说点什么吧...',
    },
    rows: {
      type: Number,
      default: 3,
    },
    maxLength: {
      type: Number,
      default: 500,
    },
    isSubmitting: {
      type: Boolean,
      default: false,
    },
    quickEmojis: {
      type: Array,
      default: () => ['😊', '👍', '❤️', '🎉', '🤔'],
    },
  },
  data() {
    return {
      commentText: '',
    }
  },
  computed: {
    canSubmit() {
      return this.commentText.trim().length > 0 && this.commentText.length <= this.maxLength
    },
  },
  methods: {
    submitComment() {
      if (this.canSubmit && !this.isSubmitting) {
        const trimmedComment = this.commentText.trim()
        this.$emit('submit-comment', trimmedComment)
        this.commentText = '' // 清空输入框
      }
    },
    addEmoji(emoji) {
      this.commentText += emoji
    },
    focus() {
      const textarea = this.$el.querySelector('.comment-input')
      if (textarea) {
        textarea.focus()
      }
    },
    clear() {
      this.commentText = ''
    },
  },
}
</script>

<style scoped>
.comment-input-area {
  display: flex;
  margin-bottom: 20px;
  padding: 15px;
  background-color: #fff;
  border-radius: 8px;
  border: 1px solid #f0f0f0;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 12px;
  flex-shrink: 0;
}

.comment-input-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.comment-input {
  width: 100%;
  min-height: 80px;
  padding: 10px;
  border: 1px solid #e5e5e5;
  border-radius: 4px;
  resize: vertical;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  color: #333;
  transition: border-color 0.3s ease;
}

.comment-input:focus {
  outline: none;
  border-color: var(--bilibili-pink, #FB7299);
}

.comment-input::placeholder {
  color: #999;
}

.comment-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
}

.emoji-actions {
  display: flex;
  gap: 8px;
}

.emoji-btn {
  background: none;
  border: 1px solid #e5e5e5;
  border-radius: 4px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.emoji-btn:hover {
  background-color: #f5f5f5;
  transform: translateY(-1px);
}

.submit-comment-btn {
  padding: 8px 24px;
  background-color: var(--bilibili-pink, #FB7299);
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
}

.submit-comment-btn:hover:not(:disabled) {
  background-color: #ff5777;
  transform: translateY(-1px);
}

.submit-comment-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
  transform: none;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .comment-input-area {
    padding: 12px;
  }
  
  .user-avatar {
    width: 36px;
    height: 36px;
    margin-right: 10px;
  }
  
  .comment-input {
    min-height: 60px;
    font-size: 14px;
  }
  
  .submit-comment-btn {
    padding: 6px 16px;
    font-size: 13px;
  }
  
  .emoji-btn {
    width: 28px;
    height: 28px;
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  .comment-actions {
    flex-direction: column;
    align-items: flex-end;
    gap: 10px;
  }
  
  .emoji-actions {
    align-self: flex-start;
  }
}
</style>